package com.blogspot.javagamexyz.gamexyz.systems;

import java.util.Arrays;
import java.util.Random;

import com.blogspot.javagamexyz.gamexyz.custom.Dice;

public class DiceRoll {

	public static int roll(int diceCount, int diceType, int modifier, boolean wildcard){
        int amount = 0;
        int roll = 0;
        Random r = new Random();
       
        if (wildcard){
            int wildRoll = r.nextInt(6) + 1;
           
            if (diceCount == 1){
                roll = r.nextInt(diceType) + 1;
                if (wildRoll > roll){
                    amount = wildRoll;
                    System.out.println("Wild (" + wildRoll + " vs " + roll + ")");	//DEBUG
                }
                else{
                    amount = roll;
                }
            }
            else{
                int[] rolls = new int[diceCount];
                for (int i=0; i<diceCount; i++){
                    rolls[i] = r.nextInt(diceType) + 1;
                    System.out.println("Roll " + (i + 1) + ": " + rolls[i]);	//DEBUG
                }
                Arrays.sort(rolls);               
                //re-roll lowest with wild die to see if it's better.
                if (wildRoll > rolls[0]){
                    System.out.println("Wild (" + wildRoll + " vs " + rolls[0] + ")");	//DEBUG
                    rolls[0] = wildRoll;                   
                }               
                for (int i=0; i<diceCount; i++){
                    amount += rolls[i];
                }               
            }
        }
       
        else{
            for (int i=0; i<diceCount; i++){
                roll =  r.nextInt(diceType) + 1;
                System.out.println("	Roll " + (i + 1) + ": " + roll); //DEBUG
                amount += roll;
            }
        }       
        //System.out.println("Rolled " + amount + " + " + modifier);
        return amount + modifier;
    }
	//Convenience methods
	public static int roll(int diceCount, int diceType, int modifier){
		return roll(diceCount, diceType, modifier, false);
	}
	public static int roll(int diceCount, int diceType){
		return roll(diceCount, diceType, 0);
	}
	public static int roll(int diceType){
		return roll(1, diceType);
	}
	public static int roll(int diceType, boolean wildcard){
		return roll(1, diceType, 0, wildcard);
	}
	public static int roll(Dice dice){
		if (dice==null)
			return 0;
		return roll(dice.getDiceCount(), dice.getDiceType(), dice.getModifier(), false);
	}
	public static int roll(Dice dice, boolean wildcard){
		if (dice==null)
			return 0;
		return roll(dice.getDiceCount(), dice.getDiceType(), dice.getModifier(), wildcard);
	}
	
	public static int wildRoll(int diceCount, int diceType, int modifier){
		return roll(diceCount, diceType, modifier, true);
	}
	public static int wildRoll(int diceCount, int diceType){
		return roll(diceCount, diceType, 0, true);
	}
	public static int wildRoll(int diceType){
		return roll(1, diceType, 0, true);
	}
	public static int wildRoll(Dice dice){
		if (dice==null)
			return 0;
		return roll(dice.getDiceCount(), dice.getDiceType(), dice.getModifier(), true);
	}
	
	/*public static int rolly(int val, int rolls){ 
		if (val<=0 || rolls <= 0) 
			return 0; 
		Random r = new Random(); 
		int total = 0; 
		int roll; 
		for (int i = 0; i<rolls; i++){ 
			roll = r.nextInt(val) + 1; 
			total += roll; 
			if (roll == val){
				i--;
				
			}
		}
		return total;
	}*/
	
}
